package com.example.springLearning.part8.config;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Service;

@Aspect
@Service
public class AopConfig8 {

    @Before("execution(* com.example.springLearning.part8.service.Electric8Service.charge())")
    public void checkAuthority(JoinPoint pjp) throws Throwable {
        System.out.println("validating user authority");
        Thread.sleep(1000);
    }

    @Around("execution(* com.example.springLearning.part8.service.Electric8Service.doCharge())")
    public void recordingPerformance(ProceedingJoinPoint pjp) throws Throwable {
        long start = System.currentTimeMillis();
        pjp.proceed();
        long end = System.currentTimeMillis();
        System.out.println("charge method time cost: " + (end - start));
    }

    @Before("execution(* com.example.springLearning.part8.service.Electric8Service.charge())")
    public void logBeforeMethod(JoinPoint joinPoint) throws Throwable {
        System.out.println("step into -> " + joinPoint.getSignature());
    }
}
